package com.woniu.darryringmall.front.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.woniu.darryringmall.front.entity.*;
import com.woniu.darryringmall.front.service.HistoryService;
import com.woniu.darryringmall.front.service.ViewGoodsService;
import com.woniu.darryringmall.utils.MallUtils;
import com.woniu.darryringmall.utils.ResponseData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("front/goods")
public class GoodsController {

    @Autowired
    private ViewGoodsService viewgoodsService;

    @Autowired
    private HistoryService historyService;


    /**
     * 展示商品详细信息
     *
     * @param gid
     * @return
     */
    @PostMapping("detail")
    public ResponseData detail(int gid, HttpSession session) {
        //增加浏览记录
        User user = (User) session.getAttribute("user");
        if (user != null) {
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("userId", user.getId());
            List<History> historys = historyService.list(queryWrapper1);
            if (historys != null) {
                for (History h : historys) {
                    //判断用户的浏览历史中是否包含当前的gid
                    if (h.getGoodsId() == gid) {
                        //包含gid,则直接改变浏览时间
                        h.setHistoryTime(MallUtils.getNow());
                        historyService.updateById(h);
                    } else {
                        //不包含gid,则要判断浏览历史中数据的个数,若是5个,则直接删除时间最早的一个再加入,若小于5个,则直接加
                        if (historys.size() > 4) {
                            List<HistoryGoods> userGoods = historyService.getHistoryByUserId(user.getId());
                            historyService.delete(userGoods.get(4).getId());
                        }
                        History history = new History();
                        history.setGoodsId(gid);
                        history.setUserId(user.getId());
                        history.setHistoryTime(MallUtils.getNow());
                        historyService.add(history);
                    }
                }
            } else {
                History history = new History();
                history.setGoodsId(gid);
                history.setUserId(user.getId());
                history.setHistoryTime(MallUtils.getNow());
                historyService.add(history);
            }
        }
        return new ResponseData(viewgoodsService.view(gid));
    }

    /**
     * 进入所有商品款项页
     *
     * @return
     */
    @PostMapping("lists")
    public ResponseData lists(Integer current, String status, ViewGoods viewGoods) {
        if (current == null) {
            current = 1;
        }
        Page<ViewGoods> page = new Page<>(current, 12);
        QueryWrapper<ViewGoods> queryWrapper = new QueryWrapper<>();
        if (viewGoods.getPrice() != null) {
            queryWrapper.ge("price", viewGoods.getMinPrice());
            queryWrapper.le("price", viewGoods.getMaxPrice());
        }
        if (viewGoods.getName() != null && viewGoods.getName().trim().length() != 0) {
            queryWrapper.like("name", viewGoods.getName());
        }
        if (status != null) {
            if (status == "1") {
                queryWrapper.orderByAsc("price");
            }
            if (status == "2") {
                queryWrapper.orderByDesc("volume");
            }
        }
        if(viewGoods.getType()!=null){
            if(viewGoods.getType().equals("1")){
                queryWrapper.eq("type","1");
            }
            if(viewGoods.getType().equals("2")){
                queryWrapper.eq("type","2");
            }
        }
        page = viewgoodsService.page(page, queryWrapper);
        return new ResponseData(page);
    }

    /**
     * 改变材质
     * @param
     * @param
     * @return
     */
    @PostMapping("specchange")
    public ResponseData specChange( GoodsVO goodsVO) {
        JSONObject jsonOne = new JSONObject(new LinkedHashMap<>());
        jsonOne.put("spec",goodsVO.getSpec());
        jsonOne.put("purity",goodsVO.getPurity());
        return new ResponseData(viewgoodsService.specChange(jsonOne.toString(), goodsVO.getGid()));
    }

    /**
     * 改变纯度
     * @param
     * @param
     * @return
     */
    @PostMapping("puritychange")
    public ResponseData purityChange( GoodsVO goodsVO) {
        JSONObject jsonOne = new JSONObject(new LinkedHashMap<>());
        jsonOne.put("spec",goodsVO.getSpec());
        jsonOne.put("purity",goodsVO.getPurity());

        return new ResponseData(viewgoodsService.purityChange(jsonOne.toString(), goodsVO.getGid()));
    }
}
